var reg_time = /\d{4}-\d{1,2}-\d{1,2}\s{1}\d{1,2}:\d{1,2}/;
var reg_page_div = /<div class="pages">.*<\/div>/;
var reg_form = /<form method=/;
var reg_form_end = /<\/form>/;
var reg_table = /<table id="pid/g;
var reg_table_end = /<!--tea\.monkeye\.cn ADD/g;
var reg_id = /this\.id\)">.*<\/a><\/cite>/;
var reg_post_time = /发表于\s+\d{4}-\d{1,2}-\d{1,2}\s{1}\d{1,2}:\d{1,2}/;
var reg_floor = /\d+楼/;
var reg_page = /<\/strong>[^\/]*/;
var reg_page_url = /href="[^"]*/;
var reg_uid = /uid=\d+/;

var body = null;
var url_prefix = null;

function sis_set_content(prefix, html) {
	url_prefix = prefix;
	body = html;
}

function sis_get_pages_html() {
	var pages = reg_page_div.exec(body);
	if (pages == null) {
		return null; // return empty array
	}
	return pages[0];
}

function sis_get_next_page_url() {
	var html = sis_get_pages_html();
	if (html == null) return null;
	var match = reg_page.exec(html);
	if (match != null) {
		html = match[0];
		match = reg_page_url.exec(html);
		if (match != null) {
			var url = match[0].substr(6);
			url = url_prefix + url;
			url = sis_escape_url(url);
			return url;
		}
	}
	return null;
}

function sis_escape_url(url) {
	url = url.replace(/&amp;/g, "&");
	return url;
}

function sis_get_posts(page) {
	var array = page.posts;
	var form_begin = reg_form.exec(body);
	var form_end = reg_form_end.exec(body);
	if (form_begin == null || form_end == null ) return; 
	var form = body.substring(reg_form.exec(body).index, reg_form_end.exec(body).index + 7);
	// reset the reg objects, because they are globally match
	var table_start = new RegExp(reg_table.source, 'g');
	var table_end = new RegExp(reg_table_end.source, 'g');
	var match = null;
	var tmp = null;
	while (table_start.test(form) == true && table_end.test(form) == true) {
		if (table_start.lastIndex < table_end.lastIndex) {
			var html = form.substring(table_start.lastIndex, table_end.lastIndex);
			var post = new Post();
			post.page = page;
			var match = reg_id.exec(html);
			if (match != null) { 
				// match will be null if a user is deleted.
				post.id = match[0].substring(10, match[0].length - 11);
			}
			match = reg_uid.exec(html);
			if (match != null) {
				post.uid = match[0];
			}
			match = reg_post_time.exec(html);
			if (match != null) {
				post.time = Date.parse(match[0]);
			}
			match = reg_floor.exec(html);
			if (match != null) {
				post.floor = parseInt(match[0].substring(0, match[0].length - 1), 10);
			}
			if (post.id != null && post.uid != null && post.time != null && post.floor != null) {
				array.push(post);
			}
			post.floor_link = sis_extract(/setcopy\('/, "'", html);
			post.url = post.floor_link;
			post.content = sis_extract(/<div id="[^"]+" class="t_msgfont">/, '</div>', html, '<div class="quote">', '</div>');
			post.mark_link = sis_extract_link('查楼', html);
			post.rate_link = sis_extract_link('评分', html);
			post.tid = sis_extract(/tid=/, '&', post.rate_link);
			post.pid = sis_extract(/pid=/, '&', post.rate_link);
			if (html.indexOf("action=viewrationgs") != -1) {
				post.socred = true;
			}
		}
	}
}

function sis_extract_link(text, html) {
	var reg = new RegExp('<a[^>]+>' + text + "<\/a>");
	var match = reg.exec(html);
	if (match == null) return null;
	return sis_extract(/href="/, '"', match[0]);
}

function sis_extract(reg, end, html, unwant_begin, unwant_end) {
	var match = reg.exec(html);
	if (match == null) return null;
	var tmp = html.substring(match.index + match[0].length);
	if (unwant_begin && unwant_end) {
		var idxb = tmp.indexOf(unwant_begin);
		if (idxb != -1) {
			var seg1 = tmp.substring(0, idx);
			var seg2 = tmp.substring(idx);
			var idxe = seg2.indexOf(unwant_end);
			if (idxe != -1) {
				seg2 = seg2.substring(idxe + unwant_end.length);
				tmp = seg1 + seg2;
			}
		}
	}
	var idx = tmp.indexOf(end);
	if (idx != -1) {
		return sis_escape_url(tmp.substring(0, idx));
	}
	return null;
}

function sis_get_title() {
	var title = /<title>.*<\/title>/.exec(body);
	if (title != null) {
		title = title[0].substring(7, title[0].length - 8);
		if (title.indexOf('-') > 0) {
			title = title.substring(0, title.indexOf('-'));
		}
		return title;
	} else
		return '';
}

function sis_get_page_number() {
	var html = sis_get_pages_html();
	if (html == null) return null;
	var match = /<strong>\d+<\/strong>/.exec(html);
	if (match != null) {
		return parseInt(match[0].substring(8, match[0].length - 9), 10);
	}
	return 0;	
}

function sis_get_formhash() {
	var formhash = /name="formhash" value="[^\"]+"/.exec(body);
	if (formhash != null) {
		return formhash[0].substring(23, formhash[0].length - 1);
	}
	return null;
}

function sis_get_formaction() {
	var post_url = /id="postform" action="post.php?[^"]+/.exec(body);
	if (post_url != null) {
		var action = post_url[0].substring(22);
		action = sis_escape_url(action);
		return url_prefix + action;
	}
	return null;
}

function sis_direct_reply(url, formhash, content, callback) {
	// formhash : get from page
	// usesig: use signature 1: use 0: not
	// subject: title
	// message: content (url encoded)
	var body = 'formhash=' + formhash + '&usesig=1&message=' + encodeURI(content);
	log(body);
	var req = new XMLHttpRequest();
	req.onload = callback;
	req.open('POST', url, true);
	req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	req.setRequestHeader("Accept-Encoding", "gzip,deflate,sdch");
	req.setRequestHeader("Accept-Language", "zh-CN,zh;q=0.8");
	req.setRequestHeader("Accept-Charset", "utf-8;q=0.7,*;q=0.3");
//	req.send(body);
}

function sis_direct_reply_form(form, url, formhash, content, callback) {
	var fh = document.getElementById("formhash");
	fh.value = formhash;
	var msg = document.getElementById("message");
	msg.value = content;
	var us = document.getElementById("usesig");
	us.value = '1';
	form.action = url;
	form.target = '_blank';
	form.submit();
}

function sis_get_working_hour() {
	var wh = /总计在线 \<span class="bold"\>.*\<\/span\> 小时, 本月在线 \<span class="bold"\>.*\<\/span\> 小时/;
	var text = wh.exec(body);
	if (text == null) {
		return null;
	}
	wh = text[0];
	var hh = /[\d\.]+/;
	var ary = hh.exec(wh);
	if (ary == null) {
		return null;
	}
	var th = ary[0];
	wh = wh.substring(ary.index + th.length);
	ary = hh.exec(wh);
	if (ary == null) 
		return null;
	var mh = ary[0];
	return '总计在线 ' + th + ' 小时, 本月在线 ' + mh + ' 小时';
}

function sis_get_threads() {
	var ret = new Array();
	var match = /版块主题/.exec(body);
	if (match == null) return ret;
	var html = body.substring(match.index);
	var thread_reg = /<tbody id=.*/;
	var thread_span_reg = /<span id="thread_/;
	var url_reg =  /<a href="[^<]+<\/a>/;
	var managed_up_reg = />[^>]+阅/;
	var managed_floor_reg = />[^>]+查至.*楼/;
	var tmp = null;
	while((match = thread_reg.exec(html)) != null) {
		var thread = new Thread();
		var idx = html.indexOf("</tbody>");
		var text = html.substring(match.index, idx);
		html = html.substring(idx + 8);
		// thread title and url
		var m2 = thread_span_reg.exec(text);
		if (m2 == null) {
			error("Cannot find thread url");
			return new Array();
		}
		tmp = text.substring(m2.index);
		idx = tmp.indexOf("</span>");
		tmp = tmp.substring(m2[0].length, idx);
		// url and title
		m2 = null;
		m2 = url_reg.exec(tmp);
		if (m2 == null) {
			error("Cannot find thread url");
			return new Array();
		}
		tmp = m2[0].substring(9, m2[0].length - 4);
		idx = tmp.indexOf('"');
		thread.url = tmp.substring(0, idx);
		idx = tmp.indexOf(">");
		thread.title = tmp.substring(idx + 1);
		// managed user and floor and page
		m2 = null;
		m2 = managed_floor_reg.exec(text);
		if (m2 == null) {
			m2 = managed_up_reg.exec(text);
			if (m2 == null) {
				// thread not being reviewed
				thread.managed_page = 0;
				thread.managed_floor = 0;
				thread.isReviewed = false;
				log(thread.title);
			} else {
				log(thread.title + "[" + m2[0].substring(1) + "]");
				thread.isReviewed = true;
			}
		} else {
			thread.isReviewed = false;
			tmp = m2[0].substring(1);
			log(thread.title + "[" + tmp + "]");
			idx = tmp.indexOf("查");
			thread.managed_user = tmp.substring(0, idx);
			tmp = tmp.substring(idx);
			if (tmp.indexOf("页") != -1) {
				m2 = null;
				m2 = /\d+/.exec(tmp);
				if (m2 != null) {
					thread.managed_page = parseInt(m2[0], 10);
					tmp = tmp.substring(m2.index + m2[0].length);
				}
			} else
				thread.managed_page = 1;
			m2 = null;
			m2 = /\d+/.exec(tmp);
			if (m2 != null) 
				thread.managed_floor = parseInt(m2[0], 10);
		}
		ret.push(thread);
	}
	return ret;
}
